package com.lzx.makeFriend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzx.common.entity.Inform;
import com.lzx.common.enums.InformType;
import com.lzx.common.utils.R;
import com.lzx.makeFriend.entity.Attention;
import com.lzx.makeFriend.entity.vo.Problem;
import com.lzx.makeFriend.entity.vo.User;
import com.lzx.makeFriend.feign.AnswerClient;
import com.lzx.makeFriend.feign.UserClient;
import com.lzx.makeFriend.mapper.AttentionMapper;
import com.lzx.makeFriend.service.AttentionService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lzx
 * @since 2022-10-24
 */
@Service
public class AttentionServiceImpl extends ServiceImpl<AttentionMapper, Attention> implements AttentionService {
    @Resource
    private AttentionMapper attentionMapper;

    @Resource
    private AnswerClient answerClient;

    @Resource
    private UserClient userClient;

    //关注其他人
    @GlobalTransactional
    @Override
    public R attention(Attention attention) {
        QueryWrapper<Attention> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", attention.getUserId()).eq("attention_user_id", attention.getAttentionUserId());
        Attention vo = attentionMapper.selectOne(wrapper);
        if (vo != null) {
            vo.setCreateTime(new Date());
            if (vo.getIsAttention() == 0) {
                vo.setIsAttention(1);
                attentionMapper.updateById(vo);

                //更新用户关注数量
                userClient.attentionAndCancelAttention(Long.valueOf(attention.getUserId()), 1, Long.valueOf(attention.getAttentionUserId()));
                return R.ok().message("取消关注成功");
            } else {
                vo.setIsAttention(0);
                attentionMapper.updateById(vo);

                //更新用户关注数量
                userClient.attentionAndCancelAttention(Long.valueOf(attention.getUserId()), 0, Long.valueOf(attention.getAttentionUserId()));
                return R.ok().message("关注成功");
            }
        } else {
            attention.setCreateTime(new Date());
            attentionMapper.insert(attention);

            //更新用户关注数量
            userClient.attentionAndCancelAttention(Long.valueOf(attention.getUserId()), 0, Long.valueOf(attention.getAttentionUserId()));

            //发送通知
            Inform inform = new Inform();
            inform.setUserId(attention.getAttentionUserId());
            User user = userClient.getUserinfoByIdToOtherServe(Long.valueOf(attention.getUserId()));
            inform.setSender(attention.getUserId());
            inform.setNickname(user.getNickname());
            inform.setAvatar(user.getAvatar());
            inform.setType(InformType.FANS.getType());
            userClient.saveInform(inform);
            return R.ok().message("关注成功");
        }
    }

    //获取当前用户已关注的人
    @Override
    public R getUserAttentionById(Long userId) {
        QueryWrapper<Attention> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<Attention> list = attentionMapper.selectList(wrapper);
        return R.ok().data("list", list);
    }

    //获取当前用户关注的人发表的问题
    @Override
    public R getUserAttentionPublishProblem(Long userId) {
        QueryWrapper<Attention> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId).eq("is_attention", 0);
        List<Attention> attentions = attentionMapper.selectList(wrapper);
        List<Integer> list = new ArrayList<>();
        for (Attention attention : attentions) {
            list.add(attention.getAttentionUserId());
        }
        if (list.size() != 0) {
            List<Problem> problems = answerClient.getUserAttentionPublishProblem(list);
            return R.ok().data("list", problems);
        } else {
            return R.ok().data("list", null);
        }
    }
}
